#include "mytcpserver.h"
#include <QDebug>

MyTcpServer::MyTcpServer() {}

MyTcpServer &MyTcpServer::getInstance()
{
    static MyTcpServer instance;
    return instance;

}

void MyTcpServer::incomingConnection(qintptr socketDescripor)
{
    qDebug()<<"new client connected";
    myTcpSocket* pTcpSocket = new myTcpSocket;
    pTcpSocket->setSocketDescriptor(socketDescripor);//关联文件描述符
    m_tcpSocketList.append(pTcpSocket);

    connect(pTcpSocket, SIGNAL(clientOff(myTcpSocket*)), this, SLOT(deleteSocket(myTcpSocket*)));

}

void MyTcpServer::deleteSocket(myTcpSocket *mySocket)
{
    QList<myTcpSocket*>::iterator iter = m_tcpSocketList.begin();
    for(;iter != m_tcpSocketList.end(); iter ++)
    {
        if(mySocket == *iter)
        {
            delete *iter;
            *iter = NULL;
            m_tcpSocketList.erase(iter);
            break;
        }
    }
    for(int i = 0;i < m_tcpSocketList.size(); i++)
    {
        qDebug()<<m_tcpSocketList.at(i)->getStrName();
    }
}
